home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Files / AsyncPB / AsyncPB.c next >
Encoding:
C/C++ Source or Header  |  1995-02-12  |  9.5 KB  |  514 lines  |  [TEXT/MPS ]

  1. /*
  2. ** AsyncPB is an example of how File System calls can be made 
  3. **    in a chain from an interrupt handler like the Time Manager.
  4. ** Once you select DoIt from the menu give it a little time to actually
  5. **    process the input file.
  6. **
  7. ** James "im" Beninghaus. DTS.
  8. */
  9.  
  10. #include    <Aliases.h>
  11. #include    <AppleEvents.h>
  12. #include    <Balloons.h>
  13. #include    <Controls.h>
  14. #include    <Desk.h>
  15. #include    <Dialogs.h>
  16. #include    <Events.h>
  17. #include    <Errors.h>
  18. #include    <Files.h>
  19. #include    <Folders.h>
  20. #include    <Fonts.h>
  21. #include    <GestaltEqu.h>
  22. #include    <Icons.h>
  23. #include    <Memory.h>
  24. #include    <Menus.h>
  25. #include     <OSUtils.h>
  26. #include    <Packages.h>
  27. #include    <Printing.h>
  28. #include    <Quickdraw.h>
  29. #include    <Resources.h>
  30. #include    <Script.h>
  31. #include    <StandardFile.h>
  32. #include    <String.h>
  33. #include    <Strings.h>
  34. #include     <LowMem.h>
  35. #include    <TextEdit.h>
  36. #include    <Timer.h>
  37. #include     <ToolUtils.h>
  38. #include    <Traps.h>
  39. #include    <Types.h>
  40. #include    <Windows.h>
  41.  
  42. #define                isStationary            2048
  43.  
  44. #define                ApplicationMenuBar        128
  45. #define                AppleMenu                128
  46. #define                AboutItem                  1
  47. #define                FileMenu                129
  48. #define                DoIt                      1
  49. #define                QuitItem                  2
  50.  
  51. #define                AboutAlert                128
  52. #define                kWindowKind                'im'
  53.  
  54. Boolean                gInForeGround            = true;
  55. Boolean                gProcessing                = true;
  56. unsigned long        gSleep                    = 0;
  57.  
  58. #ifdef powerc
  59. QDGlobals qd;
  60. #endif
  61.  
  62. void                DoMenu                    (short menu, short item);
  63. void                EventLoop                (void);
  64. int                    main                    (void);
  65.  
  66.  
  67. typedef    struct MyParamBlockRec
  68. {
  69.     IOParam               ioParam;
  70.     
  71.     IOCompletionUPP       setFPosIOCompletion;
  72.     IOCompletionUPP       readIOCompletion;
  73.     IOCompletionUPP       writeIOCompletion;
  74.     
  75.     short               readFileRefNum;
  76.     short               writeFileRefNum;
  77.     
  78.     TMTaskPtr           tm;
  79.     
  80. } MyParamBlockRec, *MyParamBlockPtr;
  81.  
  82.  
  83.  
  84.  
  85. typedef    struct TaskRec
  86. {
  87.    TMTask           theTMTask;
  88.    MyParamBlockPtr    pb;
  89.     
  90. } MyTaskRec, *MyTaskPtr;
  91.  
  92.  
  93.  
  94.  
  95. MyTaskRec            gTM;
  96. MyParamBlockRec        gPB;
  97. char                gBuffer;
  98.  
  99.  
  100. #ifndef powerc
  101. MyParamBlockPtr        GetParmBlockPtr()        = { 0x2008 };     // move.l a0, d0 ;move the pointer to where MPW C places function results
  102. MyTaskPtr            GetTMTaskPtr()            = { 0x2009 };    // move.l a1, d0 ;move the pointer to where MPW C places function results
  103. #endif
  104.  
  105. #ifdef powerc
  106. pascal void PBWriteIOCompletion(ParmBlkPtr parmblkptr)
  107. #else
  108. pascal void PBWriteIOCompletion()
  109. #endif
  110.  
  111. {
  112.     MyParamBlockPtr        pb;
  113.     
  114.     #ifdef powerc
  115.     pb = (MyParamBlockPtr)parmblkptr;
  116.     #else
  117.     pb = GetParmBlockPtr();
  118.     #endif
  119.     
  120.     if (noErr == (*pb).ioParam.ioResult)
  121.     {
  122.         PrimeTime((QElemPtr) (*pb).tm, 1000 * 1);
  123.     }
  124. }
  125.  
  126.  
  127. #ifdef powerc
  128. pascal void PBReadIOCompletion(ParmBlkPtr parmblkptr)
  129. #else
  130. pascal void PBReadIOCompletion()
  131. #endif
  132. {    
  133.  
  134.     MyParamBlockPtr        pb;
  135.  
  136.     #ifdef powerc
  137.     pb = (MyParamBlockPtr)parmblkptr;
  138.     #else
  139.     pb = GetParmBlockPtr();
  140.     #endif
  141.     
  142.     if (noErr == (*pb).ioParam.ioResult)
  143.     {
  144.         (*pb).ioParam.ioCompletion    = (*pb).writeIOCompletion;
  145.         (*pb).ioParam.ioRefNum        = (*pb).writeFileRefNum;
  146.         (*pb).ioParam.ioReqCount    = (*pb).ioParam.ioActCount;
  147.         (*pb).ioParam.ioPosMode        = fsAtMark;
  148.         (*pb).ioParam.ioPosOffset    = 0;
  149.         PBWriteAsync((ParmBlkPtr) pb);
  150.     }
  151. }
  152.  
  153.  
  154.  
  155. #ifdef powerc
  156. pascal void PBSetFPosIOCompletion(ParmBlkPtr parmblkptr)
  157. #else
  158. pascal void PBSetFPosIOCompletion()
  159. #endif
  160.  
  161. {   
  162.   
  163.     MyParamBlockPtr        pb;
  164.     
  165.     #ifdef powerc
  166.     pb = (MyParamBlockPtr)parmblkptr;
  167.     #else
  168.     pb = GetParmBlockPtr();
  169.     #endif
  170.  
  171.     if (noErr == (*pb).ioParam.ioResult)
  172.     {
  173.         (*pb).ioParam.ioCompletion    = (*pb).readIOCompletion;
  174.         (*pb).ioParam.ioRefNum        = (*pb).readFileRefNum;
  175.         (*pb).ioParam.ioReqCount    = 1;
  176.         (*pb).ioParam.ioPosMode        = fsAtMark;
  177.         (*pb).ioParam.ioPosOffset    = 0;
  178.         PBReadAsync((ParmBlkPtr) pb);
  179.     }
  180. }
  181.  
  182. #ifdef powerc
  183. pascal void TMTaskProc(MyTaskPtr tm)
  184. #else
  185. pascal void TMTaskProc()
  186. #endif
  187. {
  188.   
  189.     /* on the PowerPC, the task record ptr is passed as a parameter */
  190.     /* in 68K we have to get it from A1 */
  191.     
  192.     #ifndef powerc
  193.         MyTaskPtr    tm;
  194.        tm = GetTMTaskPtr(); 
  195.     #endif
  196.     
  197.     (*(*tm).pb).ioParam.ioCompletion    = (*(*tm).pb).setFPosIOCompletion;
  198.     (*(*tm).pb).ioParam.ioRefNum        = (*(*tm).pb).readFileRefNum;
  199.     (*(*tm).pb).ioParam.ioPosMode        = fsFromMark;
  200.     (*(*tm).pb).ioParam.ioPosOffset        = 2;
  201.     PBSetFPosAsync((ParmBlkPtr) (*tm).pb);
  202. }
  203.  
  204. void DoMenu(short menu, short item)
  205. {
  206.     OSErr                error        = noErr;
  207.     Str255                daName;
  208.     short                daRefNum;
  209.     Point                where        = { 30, 10 };
  210.  
  211.     switch (menu)
  212.     {
  213.         case AppleMenu :
  214.         {
  215.             switch (item)
  216.             {
  217.                 case AboutItem :
  218.                 {
  219.                     Alert(AboutAlert, nil);
  220.                 }
  221.                 break;
  222.                 
  223.                 default :
  224.                 {
  225.                     GetItem(GetMHandle(AppleMenu), item, daName);
  226.                     daRefNum = OpenDeskAcc(daName); 
  227.                 }
  228.                 break;
  229.             }
  230.         }
  231.         break;
  232.         
  233.         case FileMenu :
  234.         {            
  235.             switch (item)
  236.             {
  237.                 case DoIt :
  238.                 {
  239.                     DisableItem(GetMHandle(FileMenu), DoIt);
  240.  
  241.                     error = FSOpen("\pinData", 0, &gPB.readFileRefNum);    
  242.                     
  243.                     error = FSDelete("\pOutData", 0);
  244.                     error = Create("\pOutData", 0, 'MPS ', 'TEXT');
  245.                     error = FSOpen("\pOutData", 0, &gPB.writeFileRefNum);
  246.                     
  247.                     gPB.ioParam.ioBuffer        = &gBuffer;
  248.                     gPB.tm                        = (TMTaskPtr) &gTM;
  249.  
  250.                     gPB.setFPosIOCompletion        = NewIOCompletionProc (PBSetFPosIOCompletion);        
  251.                     gPB.readIOCompletion        = NewIOCompletionProc (PBReadIOCompletion);        
  252.                     gPB.writeIOCompletion        = NewIOCompletionProc (PBWriteIOCompletion);        
  253.                     
  254.                 #if false
  255.  
  256.                     gPB.ioParam.ioCompletion    = gPB.setFPosIOCompletion;
  257.                     gPB.ioParam.ioRefNum        = gPB.readFileRefNum;
  258.                     gPB.ioParam.ioPosMode        = fsFromMark;
  259.                     gPB.ioParam.ioPosOffset        = 3;
  260.                     PBSetFPosAsync((ParmBlkPtr) &gPB);
  261.                 #else
  262.                     gTM.theTMTask.qType    = 0;
  263.                     gTM.theTMTask.tmCount    = 0;
  264.  
  265.                     gTM.theTMTask.tmAddr    = NewTimerProc(TMTaskProc);
  266.                     gTM.pb        = &gPB;
  267.  
  268.                     InsTime((QElemPtr) &gTM);
  269.                     
  270.                     PrimeTime((QElemPtr) &gTM, 30 * 1);
  271.                 #endif
  272.                 
  273.                 }
  274.                 break;
  275.                                 
  276.                 case QuitItem :
  277.                 {
  278.                     RmvTime((QElemPtr)&gTM);
  279.                     gProcessing = false;
  280.                 }
  281.                 break;
  282.             }
  283.         }
  284.         break;
  285.         
  286.         default :
  287.         {
  288.         }
  289.         break;
  290.         
  291.     }
  292. }
  293.         
  294. void EventLoop()
  295. {
  296.     OSErr                error         = noErr;
  297.     EventRecord            event;
  298.     Boolean                gotEvent;
  299.     WindowPtr            window;
  300.     DialogPtr            dialog;
  301.     short                itemHit;
  302.     short                part;
  303.     long                menuItem;
  304.     short                menu;
  305.     short                item;
  306.  
  307.     gotEvent = WaitNextEvent(everyEvent, &event, gSleep, nil);
  308.     
  309.     if (IsDialogEvent(&event))
  310.     {
  311.         if (! ((keyDown == event.what || autoKey == event.what) && (event.modifiers & cmdKey)))
  312.         {
  313.             if (DialogSelect(&event, &dialog, &itemHit))
  314.             {
  315.             }
  316.         }
  317.     }
  318.     else
  319.     {
  320.         switch (event.what)
  321.         {
  322.             case nullEvent:
  323.             {
  324.             }
  325.             break;
  326.                         
  327.             case mouseDown:
  328.             {
  329.                 switch (part = FindWindow(event.where, &window))
  330.                 {
  331.                     case inMenuBar:
  332.                     {
  333.                         menuItem = MenuSelect(event.where);
  334.                         menu = HiWord(menuItem);
  335.                         item = LoWord(menuItem);
  336.                         DoMenu(menu, item);
  337.                         HiliteMenu(0);
  338.                     }
  339.                     break;
  340.     
  341.                     case inContent :
  342.                     {
  343.                         if (! (*(WindowPeek)window).hilited)
  344.                         {
  345.                             SelectWindow(window);
  346.                         }
  347.                         else
  348.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  349.                         {
  350.                         }
  351.                     }
  352.                     break;
  353.                     
  354.                     case inGrow :
  355.                     {
  356.                         #define        kMinWidth        128
  357.                         #define        kMinWeight        64
  358.                         auto        Rect            limitRect;
  359.                         auto        long            newSize;
  360.                         
  361.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  362.                         {
  363.                             limitRect = qd.screenBits.bounds;
  364.                             InsetRect(&limitRect, 8, 8);
  365.                             limitRect.top    = kMinWeight;
  366.                             limitRect.left    = kMinWidth;
  367.                             newSize = GrowWindow(window, event.where, &limitRect);
  368.                             if (0 != newSize)
  369.                             {
  370.                                 SizeWindow(window, LoWord(newSize), HiWord(newSize), true);
  371.                             }
  372.                         }
  373.                     }
  374.                     break;
  375.                     
  376.                     case inDrag:
  377.                     {
  378.                         DragWindow(window, event.where, &qd.screenBits.bounds);
  379.                     }
  380.                     break;
  381.                     
  382.                     case inGoAway :
  383.                     {
  384.                         if (TrackGoAway(window, event.where))
  385.                         {
  386.                         }
  387.                     }
  388.                     break;
  389.                 
  390.                     case inZoomIn :
  391.                     case inZoomOut :
  392.                     {
  393.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  394.                         {
  395.                             if ( TrackBox(window, event.where, part) )
  396.                             {
  397.                                 SetPort(window);
  398.                                 EraseRect(&window->portRect);
  399.                                 ZoomWindow(window, part, true);
  400.                                 InvalRect(&window->portRect);
  401.                             }
  402.                         }
  403.                     }
  404.                     break;
  405.                     
  406.                 }
  407.             }
  408.             break;
  409.             
  410.             case updateEvt:
  411.             {
  412.                 window = (WindowPtr) event.message;
  413.                 if (kWindowKind == (*(WindowPeek)window).windowKind)
  414.                 {
  415.                     SetPort(window);
  416.                     BeginUpdate(window);
  417.                     {
  418.                     }
  419.                     EndUpdate(window);
  420.                 }
  421.             }
  422.             break;
  423.             
  424.             case activateEvt:
  425.             {
  426.                 window = (WindowPtr) event.message;
  427.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  428.                 {
  429.                 }
  430.             }
  431.             break;
  432.             
  433.             case autoKey:        
  434.             case keyDown:
  435.             {
  436.                 auto    char    key;
  437.                 
  438.                 key = event.message & charCodeMask;
  439.                 if (event.modifiers & cmdKey)
  440.                 {
  441.                     menuItem = MenuKey(key);
  442.                     menu = HiWord(menuItem);
  443.                     item = LoWord(menuItem);
  444.                     DoMenu(menu, item);
  445.                 }
  446.                 else
  447.                 {
  448.                     window = FrontWindow();
  449.                     if (kWindowKind == (*(WindowPeek)window).windowKind)
  450.                     {
  451.                     }
  452.                 }
  453.             }
  454.             break;
  455.             
  456.             case osEvt:
  457.             {
  458.                 switch ((event.message >> 24) & 0x0FF)
  459.                 {
  460.                     case suspendResumeMessage:
  461.                     {
  462.                         gInForeGround = 0 != (event.message & resumeFlag);
  463.                     }
  464.                     break;
  465.                 }
  466.             }
  467.             break;
  468.             
  469.             case kHighLevelEvent:
  470.             {
  471.                 error = AEProcessAppleEvent(&event);
  472.             }
  473.             break;
  474.         }
  475.     }
  476. }
  477.  
  478. main ()
  479. {            
  480.     OSErr                error         = noErr;
  481.     Handle                menuBar;
  482.     
  483.     InitGraf(&qd.thePort);
  484.     InitFonts();
  485.     InitWindows();
  486.     InitMenus();
  487.     TEInit();
  488.     InitDialogs(nil);
  489.     InitCursor();
  490.  
  491.  
  492.     menuBar = GetNewMBar(ApplicationMenuBar);
  493.     if (nil == menuBar)
  494.     {
  495.         error = ResError();
  496.         if (noErr == error)
  497.         {
  498.             error = resNotFound;
  499.             
  500.             ExitToShell();
  501.         }
  502.     }
  503.     else
  504.     {
  505.         SetMenuBar(menuBar);
  506.         DisposHandle(menuBar);
  507.         AddResMenu(GetMHandle(AppleMenu), 'DRVR');
  508.         DrawMenuBar();
  509.     }
  510.     
  511.         
  512.     while (gProcessing)
  513.         EventLoop();
  514. }